package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.RadioGroup;

import com.shengrongdai.srd.R;
import com.srd.phone.fragment.ObligationsFragment;
import com.srd.phone.fragment.TransferFragment;
import com.srd.phone.utils.Constants;

/**
 * 债权管理
 */
public class ObligationsManageActivity extends BaseActivity implements  View.OnClickListener {

    private RadioGroup rg_manage = null;
    public FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_manage);

        initView();
        initData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        tv_common_title.setText("债权管理");
        rg_manage = (RadioGroup) findViewById(R.id.rg_manage);
        fm = getSupportFragmentManager();

    }

    public void initData() {

        rg_manage.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_transfer:
                        hideFragmentObligations();
                        showFragmentTransfer();
                        break;
                    case R.id.rb_obligations:
                        hideFragmentTransfer();
                        showFragmentObligations();
                        break;
                }
            }
        });

        showFragmentTransfer();
    }

    /** 转让管理 */
    private void showFragmentTransfer() {
        TransferFragment fragment = (TransferFragment) fm.findFragmentByTag(Constants.TRANSFER_MANAGE);
        if (fragment == null) {
            fragment = new TransferFragment();
            fm.beginTransaction().add(R.id.layout_fragment, fragment, Constants.TRANSFER_MANAGE).commitAllowingStateLoss();
        }
        fm.beginTransaction().show(fragment).commitAllowingStateLoss();
    }

    private void hideFragmentTransfer() {
        TransferFragment fragment = (TransferFragment) fm.findFragmentByTag(Constants.TRANSFER_MANAGE);
        if (fragment != null) {
            fm.beginTransaction().hide(fragment).commitAllowingStateLoss();
        }
    }


    /** 债权管理 */
    private void showFragmentObligations() {
        ObligationsFragment fragment = (ObligationsFragment) fm.findFragmentByTag(Constants.OBLIGATIONS_MANAGE);
        if (fragment == null) {
            fragment = new ObligationsFragment();
            fm.beginTransaction().add(R.id.layout_fragment, fragment, Constants.OBLIGATIONS_MANAGE).commitAllowingStateLoss();
        }
        fm.beginTransaction().show(fragment).commitAllowingStateLoss();
    }

    private void hideFragmentObligations() {
        ObligationsFragment fragment = (ObligationsFragment) fm.findFragmentByTag(Constants.OBLIGATIONS_MANAGE);
        if (fragment != null) {
            fm.beginTransaction().hide(fragment).commitAllowingStateLoss();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}